package com.hqj.utils;

import java.security.MessageDigest;

public class StringUtil {

    public static void main(String[] args) {
        String code = "123456";
        System.out.println(md5(code));
    }

    /**
     * md5加密
     * @param code
     * @return
     */
    public static String md5(String code){
        try{
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(code.getBytes());
            int i;
            StringBuffer sb = new StringBuffer();
            for(byte b:md.digest()){
                i = b;
                if(i<0){
                    i += 256;
                }
                if(i<16){
                    sb.append(0);
                }
                sb.append(Integer.toHexString(i));
            }
            //return sb.toString();//32位的加密
            return sb.toString().substring(8,24);//16位的加密

        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

}
